#include <iostream>
#include <queue>

using namespace std;

class Solution {
private:
    struct TreeNode {
        TreeNode* children[26] = { nullptr };
    };
public:
    int minimumLengthEncoding(vector<string>& words) {
        TreeNode root;
        for (string& word : words) {
            TreeNode* node = &root;
            for (int i = word.size() - 1; i >= 0; i--) {
                if (node->children[word[i] - 'a'] == nullptr) {
                    node->children[word[i] - 'a'] = new TreeNode;
                }
                node = node->children[word[i] - 'a'];
            }
        }
        queue<TreeNode*> queue;
        queue.push(&root);
        int cur_len = 1;
        int ans = 0;
        while (queue.size() > 0) {
            for (int len = queue.size() - 1; len >= 0; len--) {
                TreeNode* node = queue.front();
                queue.pop();
                bool has_child = false;
                for (int i = 0; i < 26; i++) {
                    if (node->children[i] != nullptr) {
                        has_child = true;
                        queue.push(node->children[i]);
                    }
                }
                if (!has_child) {
                    ans += cur_len;
                }
            }
            cur_len++;
        }
        return ans;
    }
};

int main() {
    vector<string> input = {"time", "me", "bell"};
    Solution solution;
    int output = solution.minimumLengthEncoding(input);
    cout << output << endl;
}